<template>
  <div id="contain" :style="{backgroundImage: 'url(' + backgroundImg + ')' }">
    <div id="login-box">
      <el-form @submit.native.prevent :model="loginForm" :rules="rules" ref="loginForm" label-width="0"
               class="demo-ruleForm">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" clearable placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" clearable show-password
                    @keypress.enter.native="submitForm('loginForm')"
                    placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">登陆</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div id="record-info">
      <p :style="{color: recordFontColor}">
        <span @click="openICP">{{recordInfo.recordNo}}</span>
        <span>&nbsp; Copyright © {{new Date().getFullYear() + ' ' + recordInfo.nickname}}. All Rights Reserved.</span>
      </p>
    </div>
  </div>
</template>

<script>
import login from './api/login'
import background from '@/util/background'
import md5 from 'md5'

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: '账号不能为空', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
        ]
      },
      background: {
        bgs: background.importPic,
        currentPic: ''
      },
      recordInfo: {
        queryUrl: 'https://beian.miit.gov.cn/',
        recordNo: '粤ICP备2021038304号',
        nickname: '趁雨行'
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let param = {
            username: this.loginForm.username,
            password: md5(this.loginForm.password)
          }
          let response = await login(param);
          if (response.success) {
            window.localStorage.setItem("tokenName", response.data.header)
            window.localStorage.setItem("token", response.data.token)
            window.localStorage.setItem("name", response.data.name)
            await this.$router.replace('/');
          }
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    openICP() {
      window.open(this.recordInfo.queryUrl, '_blank')
    }
  },
  computed: {
    backgroundImg() {
      return require('@/assets/background/' + this.background.currentPic);
    },
    recordFontColor() {
      return background.oppositeColor(this.background.currentPic);
    }
  },
  mounted() {
    let query = this.$route.query;
    if (query.u) {
      this.loginForm.username = query.u;
    }
    if (query.p) {
      this.loginForm.password = query.p;
    }
  },
  created() {
    let index = Math.floor(Math.random() * this.background.bgs.length);
    this.background.currentPic = this.background.bgs[index]
  }
}
</script>

<style scoped lang="less">
#contain {
  height: 100%;
  width: 100%;
  background-size: 100% 100%;


  #login-box {
    position: absolute;
    width: 15%;
    height: 10%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 8% 1%;
    border-radius: 5%;
    background-color: rgba(60, 60, 60, 0.8);
  }

  #record-info {
    position: absolute;
    bottom: 3px;
    left: 50%;
    transform: translate(-50%, 0%);

    > p > span:first-child {
      text-decoration: underline;
      cursor: pointer;
    }
  }
}


</style>
